package com.szholly.pro.watergas_server.ps.android.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletContext;

import com.szholly.data.general.RowBase;
import com.szholly.pro.watergas_server.ps.android.common.*;
import com.szholly.pro.watergas_server.ps.data.OperationGroupRow;
import com.szholly.utils.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.ServletContextAware;

/**
 * 地表水控制器
 * 
 * 2017/3/30 <br/>
 *
 * @author Joey Huang QQ:273179370）
 */
@Controller
@RequestMapping("/android/surface/*")
public class SurfaceControl implements ServletContextAware {
	public static final DomainBean DAILY_CHECK = new DomainBean("e1fc59ca-63ea-4809-b95d-e7e0226705b2", "日常巡检");
	public static final DomainBean SUPPLIES_REPLACE = new DomainBean("2c8997f4-c12c-424e-b1c8-71d516c412fc", "备件耗材更换");
	public static final DomainBean DAILY_CALIBRATION = new DomainBean("643c6771-ebf6-4213-ace7-ac9cdd842539", "日常校准");
	public static final DomainBean EQUIPMENT_REPAIR = new DomainBean("263f496f-c27c-4676-87b5-55dbc17f10ef", "设备故障及维修");
	public static final DomainBean STANDARD_CHECK = new DomainBean("315b468b-93e0-4064-989e-fb9b10cfe823", "标样核查");
	public static final DomainBean WATER_COMPARISON = new DomainBean("c1586161-9422-437c-a095-1885c84f3f69", "水样比对");
	public static final DomainBean STANDARD_REPLACE = new DomainBean("fa85f7c6-bf85-431e-b1a2-3765224f07ea", "标准物质更换");

	// public static final String[] DBSJOB_IDS = new String[]{
	// "e1fc59ca-63ea-4809-b95d-e7e0226705b2",
	// "2c8997f4-c12c-424e-b1c8-71d516c412fc",
	// "c1586161-9422-437c-a095-1885c84f3f69",
	// "643c6771-ebf6-4213-ace7-ac9cdd842539"
	// , "315b468b-93e0-4064-989e-fb9b10cfe823",
	// "fa85f7c6-bf85-431e-b1a2-3765224f07ea",
	// "263f496f-c27c-4676-87b5-55dbc17f10ef",
	// "21c7cb0d-9ea5-46d1-b150-c04151386678"
	// , "d8dfaab4-79b5-4ebd-adb5-df65b334c70f", "123456"};
	// public static final String[] JOB_DBS_NAMES = {"日常巡检", "备件耗材更换", "水样比对",
	// "日常校准",
	// "标样核查", "标准物质更换", "设备故障及维修", "人工监测",
	// "现场考核", "异常处理"};

	@SuppressWarnings("unused")
	private ServletContext servletContext;

	@Override
	public void setServletContext(ServletContext arg0) {
		this.servletContext = arg0;
	}

	/**
	 * 获取界面数据
	 */
	@RequestMapping("/getStationData")
	@ResponseBody
	public String getStationData(String token, String userId) {
		try {
			ValidUtils.validateToken(token);
			ValidUtils.hasText(userId, "用户ID不能为空");
			RowBase groupRow = CommonSqlUtils.getGroupRow(userId);
			String groupCode = groupRow == null ? null : groupRow.getSafeString(OperationGroupRow.FIELD_GUID);
			List<RowBase> list = SurfaceService.INSTANCE.getStationList(groupCode);
			return ResultUtils.getToastResult(list);
		} catch (Exception e) {
			return ResultUtils.getToastResult(e);
		}
	}

	/**
	 * 获取任务类型列表
	 */
	@RequestMapping("/getTaskData")
	@ResponseBody
	public String getTaskData(String token, String userId, String stationCode) {
		try {
			ValidUtils.validateToken(token);
			ValidUtils.hasText(userId, "用户ID不能为空");
			ValidUtils.hasText(stationCode, "站点编号不能为空");
			List<DomainBean> taskList = new ArrayList<>();
			taskList.add(DAILY_CHECK);
			taskList.add(SUPPLIES_REPLACE);
			taskList.add(DAILY_CALIBRATION);
			taskList.add(EQUIPMENT_REPAIR);
			taskList.add(STANDARD_CHECK);
			taskList.add(WATER_COMPARISON);
			taskList.add(STANDARD_REPLACE);
			Map<String, String> taskCountMap = SurfaceService.INSTANCE.getTaskCountMap(stationCode);			
			for (DomainBean taskBean : taskList) {
				String jobId = taskBean.getId();
				String count = taskCountMap.get(jobId);
				if (StringUtils.IsNullOrSpace(count)) {
					taskBean.setCount("0");
				}else{
					taskBean.setCount(count);
				}				
			}	
			
			
			// 获取签到状态
			boolean needSignIn = false;
			SecurityConfig securityConfig = SecurityConfig.get(userId);
			if (!securityConfig.containSurfaceNeedNotSignIn) {
				DateTime lastSignInTime = CommonSqlUtils.getLastSignInTime(userId, stationCode);
				DateTime today = DateTime.getToday();
				if (lastSignInTime.getMillis() < today.getMillis()) {
					needSignIn = true;
				}
			}

			Map<String, Object> resultMap = new HashMap<>();
			resultMap.put("needSignIn", needSignIn);
			resultMap.put("taskList", taskList);
			return ResultUtils.getToastResult(resultMap);
		} catch (Exception e) {
			return ResultUtils.getToastResult(e);
		}
	}

}
